home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / gfx / misc / FujiControl.lha / SerialPort.c < prev   
Encoding:
C/C++ Source or Header  |  1999-06-05  |  4.5 KB  |  165 lines

  1. /************ defines for serial port *****/
  2. #include <devices/serial.h>
  3. #define SERIALDEVICE "serial.device"
  4. #define SERIALUNIT 0
  5. struct MsgPort *serialreplyport;
  6. struct IOExtSer *serial_req;
  7. int serialopen;
  8. #define MAXLEN 8192
  9. unsigned char buffer[MAXLEN];
  10. char rxbuffer[MAXLEN];
  11.  
  12.  
  13.  
  14. /************ functions for serial port **/
  15.  
  16. /***** Open the serial port - returns 1 if ok, 0 if error. in case of error all resources opened by this function are released */
  17. openserial(baudrate)
  18. {    /*  Open a message port */
  19.     serialreplyport = (struct MsgPort *) CreatePort( NULL, 0 );
  20.      if( !serialreplyport )
  21.     {    printf("Couldnt create message port \n");
  22.         closeserial();
  23.         return(0);
  24.     }
  25.  
  26.     /*  Allocate a request block of IoExtSer type */
  27.     serial_req = (struct IOExtSer *)
  28.         CreateExtIO( serialreplyport, sizeof( struct IOExtSer ) );
  29.  
  30.     if( !serial_req )
  31.     {    printf("Couldnt allocate request block \n");
  32.         closeserial();
  33.         return(0);
  34.     }
  35.  
  36.  
  37.     /*  Set flags and open device */
  38.      serial_req->io_SerFlags = SERF_SHARED|SERF_7WIRE;
  39.     serialopen=1;
  40.     if (OpenDevice( SERIALDEVICE, SERIALUNIT, serial_req, 0 ))
  41.     {    printf("Couldnt open the device \n");
  42.         serialopen=0;
  43.         closeserial();
  44.         return(0);
  45.     }
  46.  
  47.     /*  Set parameters */
  48.     serial_req->io_RBufLen = 16384;    /* Set size of receive buffer */
  49.     serial_req->io_Baud = baudrate;        /* Baud rate */
  50.     serial_req->io_BrkTime = 500000;    /* Set break length */
  51.     serial_req->io_ReadLen = 8;    /* Set word length to 8 bit */
  52.     serial_req->io_WriteLen = 8;        /* Set write word length */
  53.     serial_req->io_StopBits = 1;        /* Set no of stop bits */
  54.     serial_req->io_SerFlags = SERF_SHARED | SERF_XDISABLED | SERF_PARTY_ON;
  55.     serial_req->io_ExtFlags = 0;
  56.     serial_req->IOSer.io_Command = SDCMD_SETPARAMS;
  57.  
  58.  
  59.     if (DoIO( serial_req ))
  60.     {    printf("setserial():Couldnt set parameters \n");
  61.         closeserial();
  62.         return(0);
  63.     }
  64.     
  65.     return(1);    
  66. }
  67.  
  68. /***** closeserial() - close the serial port and release all resources used by it *****/
  69. closeserial()
  70. {    
  71.     /* Close the serial port (if it was succesfully open that is) */
  72.     if (serialopen) {CloseDevice( serial_req); serialopen=NULL;};
  73.  
  74.     /* Deallocate the serial request block (if it was succesfully allocated) */
  75.     if( serial_req )
  76.     {    DeleteExtIO( serial_req, sizeof( struct IOExtSer ) ); serial_req=NULL;}
  77.     
  78.     /* Remove the reply message port (if it exists) */
  79.     if( serialreplyport )
  80.     {    DeletePort( serialreplyport); serialreplyport=NULL;}
  81.     return(1);
  82. }
  83.  
  84.  
  85. /***** Return the number of chars waiting in the buffer or -1 if error */
  86. serchars()
  87. {    serial_req->IOSer.io_Command = SDCMD_QUERY;
  88.     if (DoIO(serial_req))
  89.         return(-1); /* could not get info */
  90.     else
  91.         return((int)serial_req->IOSer.io_Actual);
  92. }
  93.  
  94. /***** read characters from serial port *****/
  95. readserial(buffer,maxlen)
  96.     char *buffer;
  97.     int maxlen;
  98. {
  99.         int numchars=0,error=0;
  100.         if (buffer==NULL) return(-1);
  101.  
  102.         /* check serial port for characters and read them */
  103.         numchars=serchars();
  104.         if (numchars>maxlen) numchars=maxlen;
  105.         if (numchars>0)
  106.         {
  107.             serial_req->IOSer.io_Command=CMD_READ;
  108.             serial_req->IOSer.io_Length = numchars;
  109.             serial_req->IOSer.io_Data = (APTR)buffer;
  110.             if (error=DoIO(serial_req)) {printf("Error %d reading port: expected %d characters\n",error,numchars);return(-999);}
  111.         }
  112.         return(numchars);
  113. }
  114.  
  115. /***** read characters from serial port, without any checking *****/
  116. flushserial(unsigned char *buff,int maxlen, int show)
  117. {
  118.         char buffer[80];
  119.         int count=0;
  120.         if (show) printf("RX-> ");
  121.         if (buff==NULL || maxlen<1) return(0);
  122.         if (serial_req==NULL) return(0);
  123.         /* check serial port for characters and read them */
  124.         while(serchars())
  125.         {
  126.             buffer[0]=0xff;
  127.             serial_req->IOSer.io_Command=CMD_READ;
  128.             serial_req->IOSer.io_Length = 1;
  129.             serial_req->IOSer.io_Data = (APTR)&buff[count];
  130.             if (DoIO(serial_req)) {if (show) printf("!");} else {if (show) printf("+");}
  131.             if (show) printf("%02x ",(int)buff[count]);
  132.             count++;
  133.             if (count>=maxlen) count=maxlen-1;
  134.             if (!serchars()) Delay(10);
  135.         }
  136.         if (show) printf("\n");
  137.         return(count);
  138. }
  139.  
  140.  
  141. /***** write characters to serial port *****/        
  142. writeserial(buffer,len)
  143.     char *buffer;
  144.     int len;
  145. {
  146.     int error;
  147.     serial_req->IOSer.io_Command=CMD_WRITE;
  148.     serial_req->IOSer.io_Length = len;
  149.     serial_req->IOSer.io_Data = (APTR)buffer;
  150.     if (error=DoIO(serial_req)) {printf("Error %d writing to port\n",error);return(-1);}    
  151.     return(len);
  152. }
  153.  
  154. /***** send a single byte to the serial port *****/
  155. sendchar(byte)
  156.     char byte;
  157. {
  158.     int error,len=1;
  159.     serial_req->IOSer.io_Command=CMD_WRITE;
  160.     serial_req->IOSer.io_Length = len;
  161.     serial_req->IOSer.io_Data = (APTR)&byte;
  162.     if (error=DoIO(serial_req)) {printf("Error %d writing to port\n",error);return(-1);}    
  163.     return(len);
  164. }
  165.